/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

var app = {
    // Application Constructor
    initialize: function () {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function () {
        this.receivedEvent('deviceready');
        init();
    },

    // Update DOM on a Received Event
    receivedEvent: function (id) {
        var parentElement = document.getElementById(id);
        /*
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');
        */

        console.log('Received Event: ' + id);
    }
};

app.initialize();

/*
if(navigator && navigator.camera){
	
}else{
	init();
}*/

function init() {
    $('#uuid').val(device.uuid);
    window.tools = {
        scan: function (callback, timeout) {

            QRScanner.scan(function (err, contents) {

                if (err) {
                    alert(err._message);
                    console.error(err._message);
                }

                if (callback) {
                    console.log(contents);
                    callback(contents);
                } else {
                    alert('The QR Code contains: ' + contents);
                    console.log('The QR Code contains: ' + contents);
                }

                window.tools.stopScan();
            });

            //透明webview的内容背景
            QRScanner.show();
            $(document.body).css({opacity: "0.5", background: 'none'});

            //超时自动关闭扫码
            if (timeout) {
                setTimeout(function () {
                    window.tools.stopScan();
                }, timeout);
            }

            /*
            QRScanner.hide(function(status){
              console.log(status);
            });*/
        },

        stopScan: function () {

            QRScanner.destroy(function (status) {
                console.log(status);
                //恢复背景
                setTimeout(function () {
                    document.body.style.removeProperty('background');
                    $(document.body).css({opacity: "1"});
                }, 500)
            });
        },

        /*NFC_Listener*/

        nfcNdefCallback: function () {
            console.log(arguments);
            $(window).trigger('nfcNdefScan', arguments[0]);
        },

        nfcTagCallback: function () {
            console.log(arguments);
            $(window).trigger('nfcTagScan', arguments[0]);
        },

        consoleLog: function () {
            console.log(arguments)
        },

        startNfc: function () {
            /*
            nfc.addNdefListener(
                window.tools.nfcNdefCallback,
                window.tools.consoleLog,
                window.tools.consoleLog
            );*/

            nfc.addTagDiscoveredListener(
                window.tools.nfcTagCallback,
                window.tools.consoleLog,
                window.tools.consoleLog
            );
        },

        stopNfc: function () {

            nfc.removeNdefListener(
                window.tools.nfcTagCallback,
                window.tools.consoleLog,
                window.tools.consoleLog
            );

            nfc.removeTagDiscoveredListener(
                window.tools.nfcTagCallback,
                window.tools.consoleLog,
                window.tools.consoleLog
            )
        }
    }

    window.tools.startNfc();

    window.onbeforeunload = function () {
        window.tools.stopNfc();
    }

    console.log('init')


    /*
    if(typeof(QRScanner) != "undefined"){
        var displayContents = function (err, text){
          if(err){
            console.log(err);
            // an error occurred, or the scan was canceled (error code `6`)
            alert('error to scan')
          } else {
            // The scan completed, display the contents of the QR code:
            alert(text);
          }
        }
        QRScanner.scan(displayContents);
        alert('scan')

    }else{
        alert('no QRScanner')
    }*/

}
